package com.changgou.search.controller;

import com.changgou.entity.Page;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @Author Tang
 * @Date 2020/9/17 20:40
 * @Description
 */
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 将查询的数据返回到前端页面进行赋值
     *
     * @param searchMap
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {
        //特殊符号处理
        //this.handleSearchMap(searchMap);

        //1 获取查询结果
        Map resultMap = searchService.search(searchMap);
        model.addAttribute("result", resultMap);
        model.addAttribute("searchMap", searchMap);


        //2 拼接url
        StringBuilder url = new StringBuilder("/search/list");
        if (searchMap != null && searchMap.size() > 0) {
            //拼接查询条件
            url.append("?");
            for (String paramKey : searchMap.keySet()) {
                //排序和分页功能点击对应按钮实现,需要排除掉
                if (!"sortRule".equals(paramKey) && !"sortField".equals(paramKey) && !"pageNum".equals(paramKey)) {
                    url.append(paramKey).append("=").append(searchMap.get(paramKey)).append("&");
                }
                //http://localhost:9009/search/list?keywords=手机&spec_网络制式=4G&
                String urlString = url.toString();
                //去除路径上的最后一个&
                urlString = urlString.substring(0, urlString.length() - 1);
                model.addAttribute("url", urlString);
            }
        } else {
            model.addAttribute("url", url);
        }

        /**
         * 3 封装分页数据并返回
         *   (1) 总记录数
         *   (2)当前页
         *   (3)每页显示条数
         */
        Page<SkuInfo> page = new Page<>(
                Long.parseLong(String.valueOf(resultMap.get("total"))),
                Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                Page.pageSize
        );
        model.addAttribute(page);
        return "search";
    }


    /**
     * 根据前端的条件查询商品数据
     *
     * @param searchMap
     * @return 内部封装了商品数据, 分组列表, 总记录数, 总页数等数据的map集合
     */
    @ResponseBody
    @GetMapping
    public Map search(@RequestParam Map<String, String> searchMap) {
        //如果规格名称上有"+",进行特殊符号处理
        //this.handleSearchMap(searchMap);
        Map searchResult = searchService.search(searchMap);

        return searchResult;
    }


    /**
     * 将规格名称带有的"+",转换成字符"%2B"
     *
     * @param searchMap
     */
    private void handleSearchMap(Map<String, String> searchMap) {
        //获取map集合中,键值对的set集合
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();

        //遍历set集合
        for (Map.Entry<String, String> entry : entries) {
            //如果该键,对应的值是以spec_开头的
            if (entry.getKey().startsWith("spec_")) {
                /*PS:谷歌浏览器会把前端传递的参数的"+"转换成" "空格,
             导致"+"不存在,故该转换方法对当下情况不适用*/
                //进行特殊符号替换,否则前台传递的参数,规格名称中有"+"的,查询时会报错
                searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
            }

        }

    }
}
